package com.project.frba.bifront.client.view.charts;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Image;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.Window;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.project.frba.bifront.client.model.Dashboard;
import com.project.frba.bifront.client.rpc.DashboardServices;
import com.project.frba.bifront.client.rpc.DashboardServicesAsync;
import com.project.frba.bifront.client.view.dashboard.ChartToDashboardWindow;

public class ChartWindow extends Composite{
	
	private Window mainWindow = new Window();
	private Button btSendToDashboard = new Button("Enviar a tablero de comandos");
	private String chartSerialized;
	
	public ChartWindow(String urlImage, String chartSerialized){
		this.chartSerialized = chartSerialized;
		mainWindow.setWidth("615");
		mainWindow.setResizable(false);
		mainWindow.setHeight("400");
		//mainWindow.setLayout(new FitLayout());
		mainWindow.setModal(true);
		mainWindow.setClosable(true);
		mainWindow.setTitle("Grafico");
		Image chartImage = new Image("./uploadServer/" + urlImage);
		mainWindow.add(chartImage);
		loadActions();
		
		mainWindow.setButtons(new Button[]{btSendToDashboard});
		initWidget(mainWindow);
		mainWindow.center();
		mainWindow.show();
	}

	private void loadActions() {
		btSendToDashboard.addListener(new ButtonListenerAdapter(){
			public void onClick(Button button, EventObject e) {
				DashboardServicesAsync dashboardServices = (DashboardServicesAsync)GWT.create(DashboardServices.class);
				AsyncCallback<List<Dashboard>> ldCallback = new AsyncCallback<List<Dashboard>>(){
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub
						
					}
					
					public void onSuccess(List<Dashboard> dashboards) {
						ChartToDashboardWindow ctWindow = new ChartToDashboardWindow(dashboards);
						ctWindow.setChartSerialized(chartSerialized);
						ctWindow.show();
					}
				};
				dashboardServices.getMyDashboards(ldCallback);
			}
		});
		
	}
	
}
